package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.GoPayBean;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GoPayActivity extends AppCompatActivity {
    @Bind(R.id.mRedaihuitui)
    AutoLinearLayout mRedaihuitui;
    @Bind(R.id.mTime)
    TextView mTime;
    @Bind(R.id.successPrice)
    TextView successPrice;
    @Bind(R.id.successWay)
    TextView successWay;
    @Bind(R.id.mUserZH)
    TextView mUserZH;
    @Bind(R.id.mKaiHuHang)
    TextView mKaiHuHang;
    @Bind(R.id.mLianHangHao)
    TextView mLianHangHao;
    @Bind(R.id.mHuiBianMa)
    TextView mHuiBianMa;
    private LoginHuancunBean loginHuancunBean;
    private int loginId1;
    private int number;
    public boolean flag = true;
    private long num;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            number--;
            //mTime.setText("0" + number);
            num = number;
            mHandler.sendEmptyMessageDelayed(0, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_go_pay);
        Intent intent = getIntent();
        String orderCode = intent.getStringExtra("orderCode");
        Gson gson = new Gson();
        //解析查找的缓存并判断
        String loginId = ACache.get(GoPayActivity.this).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            loginId1 = loginHuancunBean.userInfo.loginId;
        }
        submitOrder(orderCode);
        ButterKnife.bind(this);
        setOnClick();
    }

    private void setOnClick() {
        mRedaihuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoPayActivity.this.finish();
            }
        });
    }

    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }

    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + ":");
        }
        if (minute > 0) {
            sb.append(minute + ":");
        }
        if (second > 0) {
            sb.append(second + "");
        }
        return sb.toString();
    }

    public void submitOrder(String orderCode) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("orderCode", orderCode);
        params.put("signLogin", SignUtil.signLogin(nonce, timestamp));
        params.put("loginId", String.valueOf(loginId1));
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.goPayUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                Log.e("onSuccess", result.toString());
                String string = (String) result;
                Gson gson = new Gson();
                GoPayBean goPayBean = gson.fromJson(string, GoPayBean.class);
                if (goPayBean.result.state.equals("1")) {
                    number = goPayBean.diff;
                    mHandler.sendEmptyMessageDelayed(0, 1000);
                    mTime.setText("0" + formatTime((long) goPayBean.diff));
                    successPrice.setText(goPayBean.payInfo.totalPrice + "");
                    successWay.setText(goPayBean.payInfo.payModeName);
                    mLianHangHao.setText(goPayBean.comAccount.unionLine);
                    mUserZH.setText(goPayBean.comAccount.account);
                    mKaiHuHang.setText(String.valueOf(goPayBean.comAccount.openingBank));
                    mHuiBianMa.setText(goPayBean.payInfo.remittanceCode);
                }
            }

            @Override
            public void onFail(Object result) {
                Log.e("zzz", "失败" + result.toString());
                System.out.print("失败");
            }
        });
    }
}
